﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Main_VideoInfo : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    VideoDao vd = new VideoDao();
    OpinionDao od = new OpinionDao();
    VideoVo vv = new VideoVo();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (em.Decript(Request.QueryString["vid"]) == null)
                {
                    Response.Redirect("../index.aspx");
                }
            }
            catch
            {
                Response.Redirect("../index.aspx");
            }
            string vid = em.Decript(Request.QueryString["vid"]);
            try
            {
                int id = Convert.ToInt32(vid);
            }
            catch
            {
                Response.Redirect("../index.aspx");
            }
            vv = vd.getSingleVideo(vid);
            if (vv != null)
            {
	        //设置Title和搜索关键词
                Page.Title =vv.Vname + " " + ConfigurationSettings.AppSettings["title"] + "-新闻 搞笑 电影 短片 原创 广告";
        	HtmlMeta metaKeyWords = new HtmlMeta();
        	HtmlMeta metaDescription = new HtmlMeta();
        	metaKeyWords.Name = "Keywords";
        	metaKeyWords.Content = ConfigurationSettings.AppSettings["Keywords"];
        	metaDescription.Name = "description";
        	metaDescription.Content = ConfigurationSettings.AppSettings["description"];
        	Header.Controls.Add(metaKeyWords);
        	Header.Controls.Add(metaDescription);
                labname.Text = vv.Vname;
                HiddenField1.Value = vv.Vlink;
                //更多视频
                dt = vd.getOtherVideo(vid,vv.Vtype);
                dlvideo.DataSource = dt;
                dlvideo.DataBind();
                for (int i = 0; i < dlvideo.Items.Count; i++)
                {
                    Image imgbtnvideo = dlvideo.Items[i].FindControl("imgbtnvideo") as Image;
                    HyperLink hlvideo = dlvideo.Items[i].FindControl("hlvideo") as HyperLink;
                    string id = em.Encript(dt.Rows[i][0].ToString());
                    hlvideo.NavigateUrl = "VideoInfo.aspx?vid=" + id;
                }
                //网友留言
                if (ViewState["pageindex"] == null)
                {
                    ViewState["pageindex"] = "0";
                    lbpre.Visible = false;
                    lbnext.Visible = false;
                }
                dt = od.getOpinion(vid, "3");
                if (dt.Rows.Count > 0)
                {
                    labnumber.Text = dt.Rows.Count.ToString();
                }
                BindData();
            }
        }
    }

    private void BindData()
    {
        string vid = em.Decript(Request.QueryString["vid"]);
        DataTable objTable = new DataTable();
        objTable = od.getOpinion(vid, "3");
        DataTable datatable = new DataTable("objTable");
        datatable.Columns.Add("留言id");
        datatable.Columns.Add("留言内容");
        datatable.Columns.Add("留言时间");
        datatable.Columns.Add("留言人");
        DataRow dr;
        if (objTable.Rows.Count > 0)
        {
            for (int i = 0; i < objTable.Rows.Count; i++)
            {
                dr = datatable.NewRow();
                dr["留言id"] = objTable.Rows[i][0].ToString();
                dr["留言内容"] = objTable.Rows[i][1].ToString();
                dr["留言时间"] = objTable.Rows[i][2].ToString();
                dr["留言人"] = objTable.Rows[i][4].ToString();
                datatable.Rows.Add(dr);
            }
        }
        if (datatable != null && datatable.Rows.Count > 0)
        {
            DataView objView = datatable.DefaultView;
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = objView;
            objPds.AllowPaging = true;
            objPds.PageSize = 10;
            objPds.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());
            if (!objPds.IsFirstPage)
            {
                lbpre.Visible = true;
            }
            else
            {
                lbpre.Visible = false;
            }
            if (!objPds.IsLastPage)
            {
                lbnext.Visible = true;
            }
            else
            {
                lbnext.Visible = false;
            }
            dlopinion.DataSource = objPds;
            dlopinion.DataBind();
            for (int i = 0; i < dlopinion.Items.Count; i++)
            {
                Label lab = (Label)dlopinion.Items[i].FindControl("lbshijian");
                DateTime t = Convert.ToDateTime(lab.Text);
                lab.Text = t.ToString("g");
                LinkButton lb = (LinkButton)dlopinion.Items[i].FindControl("lbtndel");
                UsersVo uv = new UsersVo();
                uv = (UsersVo)Session["user"];
                if (uv != null)
                {
                    if (uv.Ugroup == "管理员")
                    {
                        lb.Visible = true;
                    }
                }
            }
            labnumber.Text = dlopinion.Items.Count.ToString();
        }
        else
        {
            lbpre.Visible = false;
            lbnext.Visible = false;
        }
    }

    protected void IndexChanging(object sender, EventArgs e)
    {
        string strCommand = ((LinkButton)sender).CommandArgument.ToString();
        int pageindex = int.Parse(ViewState["pageindex"].ToString());
        if (strCommand == "pre")
        {
            pageindex = pageindex - 1;
        }
        else
        {
            pageindex = pageindex + 1;
        }
        ViewState["pageindex"] = pageindex;
        BindData();
    }

    protected void dlopinion_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string type = e.CommandName;
        int index = e.Item.ItemIndex;
        if (type == "delete")
        {
            Label lblID = e.Item.FindControl("lblID") as Label;
            string oid = lblID.Text;
            Exception ex = new OpinionDao().delOpinion(oid);
            string script = "";
            if (ex == null)
            {
                script = "alert('删除成功！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
                BindData();
            }
            else
            {
                script = "alert('删除失败！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            }
        }
    }
    protected void btntijiao_Click(object sender, EventArgs e)
    {
        string vid = em.Decript(Request.QueryString["vid"]);
        string Vid = Request.QueryString["vid"];
        if (txtcontent.Text == "")
        {
            string script = "alert('评论内容不能为空！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
        }
        else
        {
            OpinionVo ov = new OpinionVo();
            ov.Ocontent = txtcontent.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>"); ;
            ov.Id = Convert.ToInt32(vid);
            ov.Otime = DateTime.Now.ToString("g");
            if (Session["user"] != null)
            {
                UsersVo uv = new UsersVo();
                uv = (UsersVo)Session["user"];
                if (uv != null)
                {
                    ov.Uid = uv.Uid;
                    ov.Oauthor = uv.Unickname;
                }
            }
            else
            {
                ov.Uid = 0;
                if (txtname.Text == "")
                {
                    ov.Oauthor = "纺织城网友";
                }
                else
                {
                    ov.Oauthor = txtname.Text;
                }
            }
            ov.Otype = 3;
            Exception ex = od.addOpinion(ov);
            if (ex == null)
            {
                string script = "alert('评论成功！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
                txtcontent.Text = "";
                txtname.Text = "";
                BindData();
            }
            else
            {
                string script = "alert('评论失败！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            }
        }
    }
}
